package com.link2room.ironhide.facade.impl;

import com.link2room.common.constant.BaseConstant;
import com.link2room.common.constant.DefaultValue;
import com.link2room.common.dto.param.base.BaseParamDto;
import com.link2room.common.dto.result.base.HotelBaseInfoDto;
import com.link2room.common.entity.BaseEntity;
import com.link2room.common.jpa.condition.ConditionDetail;
import com.link2room.common.jpa.condition.L2RCondition;
import com.link2room.common.jpa.criteria.CriteriaUtil;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.*;
import com.link2room.ironhide.dto.result.manager.L2RServiceDto;
import com.link2room.ironhide.constant.IronhideMnsConstant;
import com.link2room.ironhide.constant.IronhideTaskNameConstant;
import com.link2room.ironhide.dto.param.inside.HotelBaseInfoInsideParamDto;
import com.link2room.ironhide.dto.param.inside.HotelMinPriceInsideParamDto;
import com.link2room.ironhide.dto.result.base.L2RHotelPictureDto;
import com.link2room.ironhide.entity.HotelMinPrice;
import com.link2room.ironhide.entity.L2RHotelBaseinfo;
import com.link2room.ironhide.entity.L2RHotelPicture;
import com.link2room.ironhide.entity.L2RHotelService;
import com.link2room.ironhide.facade.IHotelInfoFacadeService;
import com.link2room.ironhide.feign.optimus.HotelMinPriceFeignService;
import com.link2room.ironhide.service.IL2RServicesService;
import com.link2room.optimus.dto.result.base.HotelMinPriceDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by sss on 2016/8/25.
 */
@Component("hotelInfoFacadeService")
public class HotelInfoFacadeServiceImpl implements IHotelInfoFacadeService {
    @Resource
    private ICommonService commonService;
    @Autowired
    private HotelMinPriceFeignService hotelMinPriceFeignService;
    @Resource
    private IL2RServicesService l2RServicesService;

    /**
     * 酒店基础信息列表
     * @param hotelBaseInfoInsideParamDto
     * @return
     * @throws Exception
     */
    @Override
    public QueryResult<HotelBaseInfoDto> listHotelBaseInfo(HotelBaseInfoInsideParamDto hotelBaseInfoInsideParamDto)throws Exception{
        QueryResult<L2RHotelBaseinfo> hotelBaseInfoResult = commonService.findAllEQ(L2RHotelBaseinfo.class,ClassConverUtil.copyProperties(hotelBaseInfoInsideParamDto,L2RHotelBaseinfo.class),hotelBaseInfoInsideParamDto.getCurrentPage(),hotelBaseInfoInsideParamDto.getPageSize());
        return ClassConverUtil.copyQueryResultToDtoQueryResult(hotelBaseInfoResult, HotelBaseInfoDto.class, false);
    }

    /**
     * 酒店基础信息
     * @param hotelBaseInfoInsideParamDto
     * @return
     * @throws Exception
     */
    @Override
    public HotelBaseInfoDto getHotelBaseInfo(HotelBaseInfoInsideParamDto hotelBaseInfoInsideParamDto)throws Exception{
        L2RHotelBaseinfo l2RHotelBaseinfo =null;
        if(hotelBaseInfoInsideParamDto.getId()!=null){
            l2RHotelBaseinfo = commonService.findOneById(L2RHotelBaseinfo.class, hotelBaseInfoInsideParamDto.getId());
        }else if(StringUtil.isNotBlank(hotelBaseInfoInsideParamDto.getLhotelCode())){
            L2RHotelBaseinfo queryLHB=new L2RHotelBaseinfo();
            queryLHB.setLhotelGroupCode(hotelBaseInfoInsideParamDto.getLhotelGroupCode());
            queryLHB.setLhotelCode(hotelBaseInfoInsideParamDto.getLhotelCode());
            l2RHotelBaseinfo=commonService.findOneEQ(L2RHotelBaseinfo.class,queryLHB);
        }
        return ClassConverUtil.copyPropertiesToDto(l2RHotelBaseinfo, HotelBaseInfoDto.class, false);
    }

    /**
     * 酒店大图
     * @param hotelBaseInfoInsideParamDto
     * @return
     * @throws Exception
     */
    @Override
    public QueryResult<L2RHotelPictureDto> getHotelBigPic(HotelBaseInfoInsideParamDto hotelBaseInfoInsideParamDto)throws Exception{
        L2RCondition l2RCondition=new L2RCondition();
        l2RCondition.setAndConditions(new ConditionDetail[]{
                new ConditionDetail(BaseEntity.BaseEntity_.lhotelGroupCode.toString(), CriteriaUtil.Operator.EQ, hotelBaseInfoInsideParamDto.getLhotelGroupCode()),
                new ConditionDetail(BaseEntity.BaseEntity_.lhotelCode.toString(), CriteriaUtil.Operator.IN, hotelBaseInfoInsideParamDto.getLhotelCodes()),
                new ConditionDetail(L2RHotelPicture.L2RHotelPicture_.categroy.toString(), CriteriaUtil.Operator.EQ, BaseConstant.HotelBaseInfoConstant.PICCATEGROY.BIGPIC)
        });
        QueryResult<L2RHotelPicture> l2rHotelPictureResult = commonService.findAllByCondition(L2RHotelPicture.class, l2RCondition);
        return ClassConverUtil.copyQueryResultToDtoQueryResult(l2rHotelPictureResult, L2RHotelPictureDto.class, false);
    }

    /**
     * 酒店多图
     * @param hotelBaseInfoInsideParamDto
     * @return
     * @throws Exception
     */
    @Override
    public QueryResult<L2RHotelPictureDto> getHotelPics(HotelBaseInfoInsideParamDto hotelBaseInfoInsideParamDto)throws Exception{
        L2RCondition l2RCondition=new L2RCondition();
        l2RCondition.setAndConditions(new ConditionDetail[]{
                new ConditionDetail(BaseEntity.BaseEntity_.lhotelGroupCode.toString(), CriteriaUtil.Operator.EQ, hotelBaseInfoInsideParamDto.getLhotelGroupCode()),
                new ConditionDetail(BaseEntity.BaseEntity_.lhotelCode.toString(), CriteriaUtil.Operator.IN, hotelBaseInfoInsideParamDto.getLhotelCodes()),
                new ConditionDetail(L2RHotelPicture.L2RHotelPicture_.categroy.toString(), CriteriaUtil.Operator.EQ, BaseConstant.HotelBaseInfoConstant.PICCATEGROY.PICS)
        });
        QueryResult<L2RHotelPicture> l2rHotelPictureResult = commonService.findAllByCondition(L2RHotelPicture.class, l2RCondition);
        return ClassConverUtil.copyQueryResultToDtoQueryResult(l2rHotelPictureResult, L2RHotelPictureDto.class, false);
    }

    /**
     * 保存最低价
     * @param hotelMinPriceInsideParamDto
     * @throws Exception
     */
    @Override
    public void saveHotelGroupMinPrice(HotelMinPriceInsideParamDto hotelMinPriceInsideParamDto) throws Exception{
        L2RHotelBaseinfo queryL2RHotelBaseInfo=new L2RHotelBaseinfo();
        queryL2RHotelBaseInfo.setLhotelGroupCode(hotelMinPriceInsideParamDto.getLhotelGroupCode());
        queryL2RHotelBaseInfo.setLhotelCode(hotelMinPriceInsideParamDto.getLhotelCode());
        QueryResult<L2RHotelBaseinfo> hotelBaseInfoDtoResult = commonService.findAllEQ(L2RHotelBaseinfo.class,queryL2RHotelBaseInfo);
        if(hotelBaseInfoDtoResult.getResultlist()!=null&&hotelBaseInfoDtoResult.getResultlist().size()>0){
            List<HotelMinPriceInsideParamDto> hotelMinPriceInsideParamDtos=new ArrayList<>();
            HotelMinPriceInsideParamDto hotelMinPriceParam=null;
            for (L2RHotelBaseinfo hotelBaseInfo:hotelBaseInfoDtoResult.getResultlist()){
                hotelMinPriceParam=new HotelMinPriceInsideParamDto();
                hotelMinPriceParam.setLhotelGroupCode(hotelBaseInfo.getLhotelGroupCode());
                hotelMinPriceParam.setLhotelCode(hotelBaseInfo.getLhotelCode());
                hotelMinPriceParam.setPbChannel(hotelMinPriceInsideParamDto.getPbChannel());
                hotelMinPriceInsideParamDtos.add(hotelMinPriceParam);
            }
            BatchUtil.beginBatchTask(IronhideMnsConstant.QUEUE_HOTELMINPRICE,
                    IronhideTaskNameConstant.SAVE_HOTEL_MINPRICE,
                    hotelMinPriceInsideParamDtos);
        }
    }

    /**
     * 保存酒店最低价
     * @param hotelMinPriceInsideParamDto
     * @throws Exception
     */
    @Override
    public void saveHotelMinPrice(HotelMinPriceInsideParamDto hotelMinPriceInsideParamDto)throws Exception{
        String generateDaysStr = SysoptionUtil.getSysOption(hotelMinPriceInsideParamDto.getLhotelGroupCode(),"MARKETING","generate_days");
       /* SysOptions querySysOptions=new SysOptions();
        querySysOptions.setLhotelGroupCode(hotelMinPriceInsideParamDto.getLhotelGroupCode());
        querySysOptions.setCategory("MARKETING");
        querySysOptions.setItem("generate_days");
        SysOptions sysOptions1=commonService.findOneEQ(SysOptions.class,querySysOptions);*/
        /*if(sysOptions1!=null){
            generateDays=Integer.parseInt(sysOptions1.getSetValue());
        }*/
        int generateDays=200;
        if(StringUtil.isBlank(generateDaysStr)){
            generateDays=Integer.parseInt(generateDaysStr);
        }
        String startDatetime= DateUtil.format(new Date(),DateUtil.webFormat);
        String endDatetime=DateUtil.format(DateUtil.addDays(new Date(),generateDays+1L),DateUtil.webFormat);
        hotelMinPriceInsideParamDto.setStartDatetime(startDatetime);
        hotelMinPriceInsideParamDto.setEndDatetime(endDatetime);
        QueryResult<HotelMinPriceDto> hotelMinPriceResutl =hotelMinPriceFeignService.hotelMinPrices(hotelMinPriceInsideParamDto.paramToMap()).getQueryResult();
        if(hotelMinPriceResutl.getResultlist()!=null&&hotelMinPriceResutl.getResultlist().size()>0){
            List<HotelMinPrice> hotelMinPriceList=ClassConverUtil.copyPropertiesList(hotelMinPriceResutl.getResultlist(),HotelMinPrice.class);
            if(hotelMinPriceList!=null&&hotelMinPriceList.size()>0){
                HotelMinPrice queryHotelMinPrice=new HotelMinPrice();
                queryHotelMinPrice.setLhotelGroupCode(hotelMinPriceInsideParamDto.getLhotelGroupCode());
                queryHotelMinPrice.setLhotelCode(hotelMinPriceInsideParamDto.getLhotelCode());
                queryHotelMinPrice.setPbChannel(hotelMinPriceInsideParamDto.getPbChannel());
                commonService.deleteAllEQ(HotelMinPrice.class,queryHotelMinPrice);
                commonService.saveList(hotelMinPriceList);
            }
        }
        BatchUtil.doBatchTaskDone(hotelMinPriceInsideParamDto.getTaskMainId(),
                hotelMinPriceInsideParamDto.getTaskId(),
                hotelMinPriceInsideParamDto.getIsBatch());

    }

    @Override
    public List<L2RServiceDto> getL2RHotelServices(BaseParamDto baseParamDto)throws Exception{
        L2RHotelService queryHS=new L2RHotelService();
        queryHS.setLhotelGroupCode(baseParamDto.getLhotelGroupCode());
        queryHS.setLhotelCode(baseParamDto.getLhotelCode());
        queryHS.setIsHalt(DefaultValue.F);
        List<L2RServiceDto> hotelServicesList = l2RServicesService.listL2RServices(baseParamDto.getLhotelGroupCode(), baseParamDto.getLhotelCode(), queryHS, L2RHotelService.class,true );
        return hotelServicesList;
    }

}
